home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / demos / OpenGL / stonehenge / atmosphere.c++ next >
C/C++ Source or Header  |  1996-11-11  |  5KB  |  169 lines

  1. /*
  2.  * (c) Copyright 1993, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software for
  6.  * any purpose and without fee is hereby granted, provided that the above
  7.  * copyright notice appear in all copies and that both the copyright notice
  8.  * and this permission notice appear in supporting documentation, and that
  9.  * the name of Silicon Graphics, Inc. not be used in advertising
  10.  * or publicity pertaining to distribution of the software without specific,
  11.  * written prior permission.
  12.  *
  13.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  14.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  15.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  16.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  17.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  18.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  19.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  20.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  21.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  22.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  23.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  24.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  25.  *
  26.  * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
  27.  * Use, duplication, or disclosure by the Government is subject to
  28.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  29.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  30.  * clause at DFARS 252.227-7013 and/or in similar or successor
  31.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  32.  * Unpublished-- rights reserved under the copyright laws of the
  33.  * United States.  Contractor/manufacturer is Silicon Graphics,
  34.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  35.  *
  36.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  37.  */
  38. #include <GL/glu.h>
  39. #include <GL/glx.h>
  40.  
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44.  
  45. #define ATMOSPHERE_EXTERN
  46. #include "atmosphere.h"
  47.  
  48. const Weather clear("Clear", 0.);
  49. const Weather foggy("Foggy", .04, .5, white, Color(.6, .6, 1), white);
  50. const Weather very_foggy("Very Foggy", .25, .5, white, white, white);
  51. const Weather rainy("Rainy", .01, 1., black, Color(.35, .35, .35), black);
  52.  
  53. Weather weathers[nweathers] = {clear, foggy, very_foggy, rainy};
  54.  
  55. const float root3_2 = sqrt(3.) / 2.;
  56.  
  57. inline float clamp(float a, float min = 0, float max = 1)
  58. {
  59.   if (a < min) return min;
  60.   else if (a > max) return max;
  61.   else return a;
  62. }
  63.  
  64. Weather::Weather()
  65. {
  66.   strcpy(name, "No name");
  67.   fog_density = 0;
  68.   fog_color = white;
  69.   fog_spread = 1;
  70.   sun_brightness = 1;
  71.   light_sun = GL_LIGHT0;
  72.   light_ambient = GL_LIGHT1;
  73.   sky_top = blue;
  74.   sky_bottom = white;
  75. }
  76.  
  77. Weather::Weather(const char *n, GLfloat fd, GLfloat fs, Color fc, 
  78.          Color s1, Color s2, GLfloat sb,
  79.          GLenum ls, GLenum la)
  80. {
  81.   strcpy(name, n);
  82.   fog_density = fd;
  83.   fog_color = fc;
  84.   fog_spread = fs;
  85.   sun_brightness = sb;
  86.   light_sun = ls;
  87.   light_ambient = la;
  88.   sky_top = s1;
  89.   sky_bottom = s2;
  90. }
  91.  
  92. Weather::~Weather()
  93. {
  94. }
  95.  
  96. Weather Weather::operator=(Weather a)
  97. {
  98.   strcpy(name, a.name);
  99.  
  100.   fog_density = a.fog_density;
  101.   fog_color = a.fog_color;
  102.   fog_spread = a.fog_spread;
  103.  
  104.   sun_brightness = a.sun_brightness;
  105.  
  106.   light_sun = a.light_sun;
  107.   light_ambient = a.light_ambient;
  108.  
  109.   sky_top = a.sky_top;
  110.   sky_bottom = a.sky_bottom;
  111.  
  112.   return *this;
  113. }
  114.  
  115. void Weather::apply(Point sun)
  116. {
  117.   Color c;
  118.  
  119.   if (fog_density != 0) {
  120.     glFogf(GL_FOG_DENSITY, fog_density);
  121.     glFogfv(GL_FOG_COLOR, fog_color.c);
  122.     glFogi(GL_FOG_MODE, GL_EXP2);
  123.   }
  124.   glLightfv(light_sun, GL_AMBIENT, black.c);
  125.   c = white;
  126.   c *= sun_brightness;
  127.   glLightfv(light_sun, GL_DIFFUSE, c.c);
  128.   glLightfv(light_sun, GL_SPECULAR, white.c);
  129.   if (sun.pt[2] >= 0.0) glEnable(light_sun);
  130.   else glDisable(light_sun);
  131.  
  132.   c = white;
  133.   c *= .25;
  134.   glLightfv(light_ambient, GL_AMBIENT, c.c);
  135.   glLightfv(light_ambient, GL_DIFFUSE, black.c);
  136.   glLightfv(light_ambient, GL_SPECULAR, black.c);
  137.   glEnable(light_ambient);
  138. }
  139.  
  140. void Weather::draw_sky(Point sun)
  141. {
  142.   Point p;
  143.   float c;
  144.   Color bottom, top;
  145.  
  146.   if (sun.pt[2] >= .5) c = 1.;
  147.   else if (sun.pt[2] >= -.5) 
  148.     c = sqrt(1. - sun.pt[2]*sun.pt[2])*.5 + sun.pt[2]*root3_2;
  149.   else c = 0;
  150.  
  151.   bottom = sky_bottom * c;
  152.   top = sky_top * c;
  153.  
  154.   /* This is drawn as a far-away quad */
  155.   glBegin(GL_QUADS);
  156.   glColor3fv(bottom.c);
  157.   glVertex3f(-1, -1, -1);
  158.   glVertex3f(1, -1, -1);
  159.   glColor3fv(top.c);
  160.   glVertex3f(1, 1, -1);
  161.   glVertex3f(-1, 1, -1);
  162.   glEnd();
  163. }
  164.  
  165. GLfloat Weather::shadow_blur()
  166. {
  167.   return clamp(fog_density * 10.);
  168. }
  169.